<template>
  <div>
    <h2>投资添加页面</h2>
    <table class="table" border="1">
      <tr>
        <td>项目Id</td>
        <td>
          <input type="text" v-model="state.I_id" />
        </td>
      </tr>
      <tr>
        <td>投资项目名称</td>
        <td>
          <input type="text" v-model="state.Iname" />
        </td>
      </tr>
      <tr>
        <td>总额度</td>
        <td>
          <input type="text" v-model="state.I_Sum" />
        </td>
      </tr>
      <tr>
        <td>已投金额</td>
        <td>
          <input type="text" v-model="state.I_Ysum" />
        </td>
      </tr>
      <tr>
        <td colspan="2"><input type="button" value="添加" @click="Add" /></td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import router from '@/router';
import axios from 'axios';
import { reactive, toRefs, onMounted } from 'vue';
let data = reactive({
  state: {
    I_id: '',
    Iname: '',
    I_Sum: '',
    I_Ysum: ''
  },
  list: {}
});
let { list, state } = toRefs(data);

function Add() {
  if(state.value.I_id=="")
{
  alert("项目ID不能为空");
  return;
}
if(state.value.Iname=="")
{
  alert("项目名称不能为空");
  return;
}
if(isNaN(state.value.I_Sum)==true)
{
  alert("总额度必须输入数字");
  return;
}
if(isNaN(state.value.I_Ysum)==true || Number(state.value.I_Ysum)<=0)
{
  alert("已投金额必须输入数字类型并大于0");
  return;
}
  axios({
    url: 'http://localhost:57328/api/Invest/PostInvestAdd',
    method: 'post',
    data: state.value
  })
    .then((res) => {
      console.log(res);
      if (res.data.data > 0) {
        alert('添加投资项目成功');
       router.push("/InvestList")
      }
      else if(res.data.data==-1)
      {
        alert('项目名称已存在');
        return;
      }
      else
      {
        alert("添加失败");
        return;
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
</script>

<style scoped></style>
